package com.zust.zst.controller.teacher;

import com.zust.zst.aop.HttpAuthChecker;
import com.zust.zst.common.dto.teacher.*;
import com.zust.zst.common.vo.ResultVO;
import com.zust.zst.common.vo.teacher.TeacherVO;
import com.zust.zst.common.vo.teacher.UpdateVO;
import com.zust.zst.teacher.service.TeacherCourseService;
import com.zust.zst.teacher.service.TestTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.zust.zst.common.enums.OperationStatus.*;

/**
 * @author gavin
 * @date 2022/11/10 20:55
 */
@RestController
@Api(tags = "教师管理：教师信息管理接口")
@Slf4j
@RequestMapping("/teacher/teacherManagement")
public class TestTeacherController {

    @Resource(name = "TeacherCourseServiceImpl")
    private TeacherCourseService teacherCourseService;

    @ApiOperation(value = "获取全部教师信息")
    @GetMapping("/showAllTeachers/{page}/{size}")
    @HttpAuthChecker
    public ResultVO<List<Teacher>> showAllTeachers(@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        int Page = page;
        int Size = size;
        List<Teacher> teachers = teacherCourseService.showAllTeachers(((Page - 1) * Size),Size);
         if (teachers != null) {
             return ResultVO.success("教师列表",teachers);
         }
         else  return ResultVO.error("查询失败");
    }

    @ApiOperation(value = "在表格中更新教师信息")
    @PostMapping("/updateTeacher")
    @HttpAuthChecker
    public ResultVO<String> updateTeacher(@RequestBody UpdateVO updateVO) {
        if(teacherCourseService.updateTeacher(updateVO) == UPDATE_FAILED.getCode()) {
            log.warn("更新失败：{}", updateVO);
            return ResultVO.error("更新失败");
        }
            log.info("更新成功：{}", updateVO);
        return ResultVO.success("更新成功", null);
    }

    @ApiOperation(value = "手动增加教师信息")
    @PostMapping("/addTeacher")
    @HttpAuthChecker
    public ResultVO<String> addTeacher(@RequestBody AddTeacherDTO addTeacherDTO) {
        if(teacherCourseService.addTeacher(addTeacherDTO) == INSERT_FAILED.getCode()) {
            log.warn("添加失败：{}", addTeacherDTO);
            return ResultVO.error("添加失败");
        }
            log.info("添加成功：{}", addTeacherDTO);
        return ResultVO.success("添加成功", null);
    }

    @ApiOperation(value = "在表格中删除教师信息")
    @GetMapping("/deleteTeacher/{uid}")
    @HttpAuthChecker
    public ResultVO<String> deleteTeacher(@PathVariable Integer uid) {
        if (teacherCourseService.deleteTeacher(uid) == DELETE_FAILED.getCode()) {
            log.warn("删除失败，id：{}",uid);
            return ResultVO.error("删除失败");
        }
            log.info("删除成功，id：{}",uid);
        return ResultVO.success("删除成功",null);
    }

    @ApiOperation(value = "通过教师工号获取教师信息")
    @PostMapping("/getTeacherById")
    @HttpAuthChecker
    public ResultVO<List<Teacher>> getTeacherById(@RequestBody GetTeacherByIdDTO dto) {
        List<Teacher> teachers = teacherCourseService.getTeacherById(dto.getUid());
        if (teachers != null) {
            return ResultVO.success(teachers);
        }
        log.warn("暂无相应教师信息！");
        return ResultVO.error("暂无相应教师信息!");
    }

    @ApiOperation(value = "通过教师姓名获取教师信息")
    @PostMapping("/getTeacherByName")
    @HttpAuthChecker
    public ResultVO<List<Teacher>> getTeacherByName(@RequestBody GetTeacherByNameDTO dto) {
        List<Teacher> teachers = teacherCourseService.getTeacherByName(dto.getName());
        if (teachers != null) {
            return ResultVO.success((teachers));
        }
        log.warn("暂无相应教师信息！");
        return ResultVO.error("暂无相应教师信息!");
    }

    @ApiOperation(value = "获取教师记录条数")
    @GetMapping("/getTotalTeacher")
    @HttpAuthChecker
    public ResultVO<Integer> getTotalTeacher() {
        int total = teacherCourseService.getTotalTeacher();
        return ResultVO.success("教师信息条数", total);
    }

}
